package com.xxx.video.login.activitys;

import android.graphics.Color;
import android.graphics.Typeface;
import android.location.Location;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.tasks.OnSuccessListener;
import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.events.LoginSuccessEvent;
import com.xxx.video.commen.utils.FontCacheUtils;
import com.xxx.video.commen.utils.LocationUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.FontTextView;
import com.xxx.video.login.beans.LoginSuccessBean;
import com.xxx.video.login.presenters.ILoginView;
import com.xxx.video.login.presenters.LoginPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginView {
    @BindView(R.id.tv_login_activity_title)
    TextView tvLoginActivityTitle;
    @BindView(R.id.et_login_activity_user_name)
    EditText etLoginActivityUserName;
    @BindView(R.id.et_login_activity_pwd)
    EditText etLoginActivityPwd;
    @BindView(R.id.tv_login_activity_login)
    TextView tvLoginActivityLogin;
    @BindView(R.id.tv_login_activity_forget_pwd)
    TextView tvLoginActivityForgetPwd;
    @BindView(R.id.tv_login_activity_user_agency)
    TextView tvLoginActivityUserAgency;

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this, this);
    }

    @Override
    public boolean isOpenEventBus() {
        return true;
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setActionBarLeftIcon(R.drawable.ic_actionbar_left_close);
        setActionBarRightTitle(R.string.title_login_activity_right_title, R.color.c_333333);
        Typeface customFont = FontCacheUtils.getInstance().getTypeface(FontTextView.ttf3, this);
        etLoginActivityUserName.setTypeface(customFont);
        etLoginActivityPwd.setTypeface(customFont);
    }

    @Override
    public void initData(Bundle extras) {
        showLayoutStatus(1);
        initUserAgency();
    }


    @OnClick({R.id.tv_login_activity_login, R.id.tv_login_activity_forget_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login_activity_login:
                LocationUtils.getLocation(this, (OnSuccessListener<Location>) location -> {
                    if (location != null) {
                        getPresenter().login(String.valueOf(location.getLongitude()), String.valueOf(location.getLatitude()));
                    } else {
                        getPresenter().login("", "");
                    }
                });
                break;
            case R.id.tv_login_activity_forget_pwd:
                getPresenter().startForgetPwdActivity();
                break;
            default:
                break;
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {

    }

    @Override
    public void onActionBarRightIconClick() {
        super.onActionBarRightIconClick();
        startRegisterActivity();
    }

    @Override
    public void initUserAgency() {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        String title = StringUtils.getInstance().getString(R.string.login_activity_user_agency_first);
        String userAgreement = StringUtils.getInstance().getString(R.string.login_activity_user_agency_second);
        String userPrivacyPolicy = StringUtils.getInstance().getString(R.string.login_activity_user_agency_third);
        ;
        String center = StringUtils.getInstance().getString(R.string.login_activity_user_agency_forth);
        String accountVerificationServiceAgreement = StringUtils.getInstance().getString(R.string.login_activity_user_agency_fifth);

        spannableStringBuilder.append(title);
        spannableStringBuilder.append(userAgreement);
        spannableStringBuilder.append(userPrivacyPolicy);
        spannableStringBuilder.append(center);
        spannableStringBuilder.append(accountVerificationServiceAgreement);

        /*
         * init userAgreement
         */
        ClickableSpan clickableSpan1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {

            }
        };
        int start1 = title.length();
        int end1 = title.length() + userAgreement.length();
        spannableStringBuilder.setSpan(clickableSpan1, start1, end1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan foregroundColorSpan1 = new ForegroundColorSpan(Color.parseColor("#01A0F1"));
        spannableStringBuilder.setSpan(foregroundColorSpan1, start1, end1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);



        /*
         * init userPrivacyPolicy
         */
        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
            }
        };
        int start2 = title.length() + userAgreement.length();
        int end2 = title.length() + userAgreement.length() + userPrivacyPolicy.length();
        spannableStringBuilder.setSpan(clickableSpan2, start2, end2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(Color.parseColor("#01A0F1"));
        spannableStringBuilder.setSpan(foregroundColorSpan2, start2, end2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        /*
         * init accountVerificationServiceAgreement
         */
        ClickableSpan clickableSpan3 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
            }
        };
        int start3 = title.length() + userAgreement.length() + userPrivacyPolicy.length() + center.length();
        int end3 = title.length() + userAgreement.length() + userPrivacyPolicy.length() + center.length() + accountVerificationServiceAgreement.length();
        spannableStringBuilder.setSpan(clickableSpan3, start3, end3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan foregroundColorSpan3 = new ForegroundColorSpan(Color.parseColor("#01A0F1"));
        spannableStringBuilder.setSpan(foregroundColorSpan3, start3, end3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvLoginActivityUserAgency.setHighlightColor(getResources().getColor(android.R.color.transparent));
        tvLoginActivityUserAgency.setMovementMethod(LinkMovementMethod.getInstance());
        tvLoginActivityUserAgency.setText(spannableStringBuilder);
    }

    @Override
    public void startRegisterActivity() {
        intent2Activity(RegisterFirstActivity.class);
    }

    @Override
    public void startForgetPwdActivity() {
        Bundle bundle = new Bundle();
        bundle.putString("userName", getUserName());
        intent2Activity(ForgetPwdFirstActivity.class, bundle);
    }

    @Override
    public String getUserName() {
        return etLoginActivityUserName.getText().toString().trim();
    }

    @Override
    public String getPwd() {
        return etLoginActivityPwd.getText().toString().trim();
    }

    @Override
    public void loginSuccess(LoginSuccessBean successOb) {
        ToastUtils.showSuccess(StringUtils.getInstance().getString(R.string.tip_login_success));
        UserManager.getInstance().putToken(successOb.token);
        UserManager.getInstance().putUserId(successOb.user.userId);
        UserManager.getInstance().putUserName(successOb.user.username);
        if (successOb.user != null) {
            UserManager.getInstance().putUserAvatar(successOb.user.picUrl);
        }
        EventBus.getDefault().post(new LoginSuccessEvent());
        finish();
    }

    @Override
    public void startInputUserNameActivity() {
        intent2Activity(InputUserNameActivity.class);
    }

    /**
     * login Success
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccess(LoginSuccessEvent loginSuccessEvent) {
        finish();
    }
}
